package com.ugoodtech.mdcc.web.controller;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.dto.PaintResponse;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.core.utils.BaseUtil;
import com.ugoodtech.mdcc.web.dto.JsonResponse;
import com.ugoodtech.mdcc.web.dto.StationLeanPaintReportResponse;
import com.ugoodtech.mdcc.web.service.StationLeanPaintReportService;
import com.ugoodtech.mdcc.web.service.UserService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright © 2013 All Rights Reserved, Ugood Technology, Inc.
 */

@Api(description = "精益KPI报表接口")
@Controller
@RequestMapping("/stationLeanPaintReport")
public class StationLeanPaintReportController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(StationLeanPaintReportController.class);
    @Autowired
    UserRepository systemUserRepository;
    @Autowired
    UserService systemUserService;

    @Autowired
    StationLeanPaintReportService stationLeanPaintReportService;

    @Autowired
    StationLeanPaintReportRepository stationLeanPaintReportRepository;

    @Autowired
    DepartmentRepository departmentRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private StationReportRepository stationReportRepository;

    @Autowired
    private UserProfileRepository userProfileRepository;


    @ApiOperation("精益KPI报表列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "data", response = User.class)
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = false, paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getAllDepartment(@ApiParam("省份") @RequestParam(value = "province", required = false) String province,
                                         @ApiParam("城市") @RequestParam(value = "city", required = false) String city,
                                         @ApiParam("维修站类型") @RequestParam(value = "category", required = false) StationCategory category,
                                         @ApiParam("品牌") @RequestParam(value = "brand", required = false) AutoBrand brand,
                                         @ApiParam("关键字") @RequestParam(value = "param", required = false) String param,
                                         @ApiParam("外勤人员关键字") @RequestParam(value = "userParam", required = false) String userParam,
                                         @ApiParam("维修站编号") @RequestParam(value = "stationNum", required = false) String stationNum,
                                         Pageable pageable) {
        isUserLogin();
        Page<StationLeanPaintReport> stationLeanPaintReport = stationLeanPaintReportService.queryReport(province, city, category, brand, param, userParam, stationNum, pageable);
//        Page<StationLeanPaintReport> stationLeanPaintReport=new PageImpl<StationLeanPaintReport>(new ArrayList<StationLeanPaintReport>());
        //        for(Department department:departments){
//            setChildren(department);
//        }
        return JsonResponse.successResponseWithPageData(BaseUtil.getDtoPage(stationLeanPaintReport, pageable, StationLeanPaintReportResponse.class));
    }

    @ApiOperation("查出这家维修站所有的精益版喷报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "report_paints", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getStationPaintReports(Pageable pageable, @RequestParam(name = "stationId") Long stationId) {
        QStationReport qStationReport = QStationReport.stationReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qStationReport.deleted.eq(false));
        builder.and(qStationReport.enabled.eq(true));
        builder.and(qStationReport.category.eq(Constants.PAINT_REPORT));
        builder.and(qStationReport.station.id.eq(stationId));
        Page<StationReport> stationReportList = stationReportRepository.findAll(builder, pageable);
        List<PaintResponse> paintResponseList = new ArrayList<>();
        for (StationReport report : stationReportList) {
            UserProfile userProfile = userProfileRepository.findByUserId(report.getUser().getId());
            PaintResponse paintResponse = new PaintResponse(report, true, userProfile.getRealName());
            User editUser = report.getEditUser();
            if (editUser != null) {
                userProfile = userProfileRepository.findByUserId(editUser.getId());
                paintResponse.setEditUserName(userProfile.getRealName());
            }
            paintResponseList.add(paintResponse);
        }
        Page<PaintResponse> paintResponsePage = new PageImpl(paintResponseList, pageable, stationReportList.getTotalElements());
        return JsonResponse.successResponseWithPageData(paintResponsePage);
    }


//    @ApiOperation("删除维修站")
//    @RequestMapping(value = "/delete", method = RequestMethod.POST)
//    public JsonResponse delete(@ApiParam(value = "用户数据库id", required = true) @RequestParam(value = "stationLeanPaintReportId") Long stationLeanPaintReportId) {
//        isUserLogin();
//        StationLeanPaintReport stationLeanPaintReport = stationLeanPaintReportRepository.findOne(stationLeanPaintReportId);
//        stationLeanPaintReport.setDeleted(true);
//        stationLeanPaintReportRepository.save(stationLeanPaintReport);
//        return JsonResponse.successResponse();
//    }


}
